package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: HuoXu
 * @Date: 2023/07/26/16:34
 * @Description: 快乐跳跳虎
 */
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void addPackge(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        //拷贝属性值到setmeal类
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //补齐数据库字段
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.addPackge(setmeal);

        //向套餐和菜品的关系表中插入数据
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(setmeal.getId());
            setMealDishMapper.add(dish);
        }
    }

    /**
     * 套餐分页查询你
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.queryByPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐的起售与停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        setmealMapper.startOrStop(status,id);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void editSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //补齐数据库字段
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        //拷贝字段到实体类
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.editSetMeal(setmeal);
        //先删除原有的菜品
        List<Long> setmealid=new ArrayList<>();
        setmealid.add(setmeal.getId());
        setMealDishMapper.deleteBySetMealId(setmealid);
        //添加新的数据
        List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes();
        if(setmealDishList!=null&&setmealDishList.size()>0){
            setmealDishList.forEach(SetmealDish ->{
                SetmealDish.setSetmealId(setmealDTO.getId());
            });
            for (SetmealDish setmealDish : setmealDishList) {
                setMealDishMapper.add(setmealDish);
            }
        }
    }

    /**
     * 修改前的数据回显
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(Long id) {
        SetmealVO setmealvo=setmealMapper.queryById(id);
        //查询套餐分类
        setmealvo.setCategoryName(categoryMapper.queryById(setmealvo.getCategoryId()));
        //查询套餐里面的菜品
        List<SetmealDish> setmealDishes=setMealDishMapper.queryById(id);
        setmealvo.setSetmealDishes(setmealDishes);
        return setmealvo;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //根据id进行判断对应的套餐是否起售，如果是在售状态则不允许删除
        for (Long id : ids) {
            SetmealVO setmealvo=setmealMapper.queryById(id);
            if(setmealvo.getStatus()== StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //删除套餐数据
        setmealMapper.deleteByIds(ids);
        //删除套餐对应的菜品数据
        setMealDishMapper.deleteBySetMealIds(ids);
    }

    //以下是客户端接口
    /**

     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**

     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}

